package com.rao.crm.service.impl;

import com.rao.crm.dao.CustomerDao;
import com.rao.crm.domain.Customer;
import com.rao.crm.domain.PageBean;
import com.rao.crm.service.CustomerService;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className CustomerServiceImpl
 * @date 2019/9/19 15:18
 * @package com.rao.crm.service.impl
 * @description
 */
@Transactional
public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao;

    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }

    @Override
    public void save(Customer customer) {
        customerDao.save(customer);
    }

    @Override
    public PageBean<Customer> findAll(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
        PageBean<Customer> pageBean = new PageBean<>();
        /*当前页*/
        pageBean.setCurrPage(currPage);
        /*页大小*/
        pageBean.setPageSize(pageSize);
        /*总记录数*/
        Integer totalCount = customerDao.findTotalCount(detachedCriteria);
        pageBean.setTotalCount(totalCount);
        /*总页数*/
        Double ceil = Math.ceil(totalCount.doubleValue() / pageSize);
        pageBean.setTotalPage(ceil.intValue());
        /*起始索引*/
        Integer startIndex = (currPage - 1) * pageSize;
        pageBean.setStartIndex(startIndex);
        /*封装对象*/
        List<Customer> list = customerDao.findAll(detachedCriteria, startIndex, pageSize);
        pageBean.setList(list);

        return pageBean;
    }

    @Override
    public Customer findById(Long cust_id) {
        return customerDao.findById(cust_id);
    }

    @Override
    public void delete(Customer customer) {
        customerDao.delete(customer);
    }

    @Override
    public void update(Customer customer) {
        customerDao.update(customer);
    }

    @Override
    public List<Customer> findAll() {
        return customerDao.findAll();
    }
}
